import { AudioManager } from "./AudioManager";
import { Constants } from "../data/Constants";
import { FishManager } from "./FishManager";
import { SkillManager } from "./SkillManager";
import { BottomManager } from "./BottomManager";
import { BaseUtil } from "../util/BaseUtil";
import { BaseValue } from "../data/BaseValue";


const { ccclass, property } = cc._decorator;

@ccclass
export default class Game extends cc.Component {

    @property({
        type: FishManager
    })
    fishManager: FishManager = null;

    @property({
        type: BottomManager
    })
    bottomManager: BottomManager = null;

    @property({
        type: cc.Sprite
    })
    background: cc.Sprite = null;

    private _scheduleNum = 1;

    onLoad() {
        // 碰撞检测系统
        let collisionManager = cc.director.getCollisionManager();
        collisionManager.enabled = true;
        // collisionManager.enabledDebugDraw = true;

        AudioManager.loadImageFromLink(this.background, 'images/back/back_2.png');
        AudioManager.playMusicFromLink(Constants.AudioSource.BACK_1, 0.2);

        cc.find('Canvas/bottom').zIndex = 2;
        cc.find('Canvas/skill_group').zIndex = 2;

        this.schedule(() => {
            this._scheduleNum++;

            if (this._scheduleNum % 3 === 0) {
                this.fishManager.createFish(2);
                this.bottomManager._openFire(BaseValue.IS_FIRE);
            }
            this.bottomManager._fireAnim();

            if (this._scheduleNum % 100 === 0) {
                if (BaseValue.IS_FROZEN === true) {
                    this.fishManager.controlMoving();
                    BaseValue.IS_FROZEN = false;
                    this._scheduleNum = 1;
                }

            }
            if (BaseValue.GOLD_COINS > 999999) {
                BaseValue.GOLD_COINS = 999999;
            }
        }, 0.1);
    }

    start() {
        this.node.on(cc.Node.EventType.TOUCH_START, this._touchStart, this);
        this.node.on(cc.Node.EventType.TOUCH_MOVE, this._touchMove, this);
        this.node.on(cc.Node.EventType.TOUCH_END, this._touchEnd, this);
    }

    private _touchStart(event: cc.Event.EventTouch) {
        this.bottomManager._openFire(true, event);

    }

    private _touchMove(event: cc.Event.EventTouch) {
        this.bottomManager._openFire(true, event);

    }

    private _touchEnd(event: cc.Event.EventTouch) {
        this.bottomManager._openFire();
    }

}
